package com.cgw.game.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @类描述 describe
 * @作者 YF
 * @创建时间 2019/12/3 10:13
 */
public class LatticeProgressView extends View {

    /**
     * 背景画笔
     * */
    private Paint mBgPaint;

    /**
     * 上下文对象
     * */
    private Context context;

    /**
     * 宽
     * */
    private int width;
    /**
     * 高
     * */
    private int height;
    /**
     * 最大进度
     * */
    private int max = 100;

    /**
     * 当前进度
     * */
    private float progress = 75;

    public LatticeProgressView(Context context) {
        this(context,null);
    }

    public LatticeProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LatticeProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    private void initView() {
        mBgPaint = new Paint();
        mBgPaint.setAntiAlias(true);
        mBgPaint.setStyle(Paint.Style.FILL);
        mBgPaint.setColor(Color.BLUE);
        mBgPaint.setStrokeWidth(DensityUtil.dip2px(context,12));
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = measureSize(widthMeasureSpec);
        height = measureJHeightSize(heightMeasureSpec);
        setMeasuredDimension(width,height);
    }
    private int measureSize(int measureSpec) {
        int length;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode == MeasureSpec.EXACTLY){
            length = size;
        }else{
            length = DensityUtil.dip2px(context,100);
            if(mode == MeasureSpec.AT_MOST){
                length = Math.min(length,size);
            }
        }
        return length;
    }

    private int measureJHeightSize(int measureSpec) {
        int length;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode == MeasureSpec.EXACTLY){
            length = size;
        }else{
            length = DensityUtil.dip2px(context,30);
            if(mode == MeasureSpec.AT_MOST){
                length = Math.min(length,size);
            }
        }
        return length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int sc = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), null, Canvas.ALL_SAVE_FLAG);
        canvas.translate(0,height/2);
        drawBg(canvas);
        drawProgress(canvas);
        drawMask(canvas);
        mBgPaint.setXfermode(null); // 还原混合模式
        canvas.restoreToCount(sc);
    }

    /**
     * 画背景
     * */
    private void drawBg(Canvas canvas) {
        mBgPaint.setColor(Color.parseColor("#6493b0"));
        canvas.drawLine(0,0,width,0,mBgPaint);
    }
    /**
     * 画进度
     * */
    private void drawProgress(Canvas canvas) {
        float step = width/100f;
        mBgPaint.setColor(Color.parseColor("#4fe4fe"));
        canvas.drawLine(0,0,step*progress,0,mBgPaint);
    }
    /**
     * 绘制间隔的平行四边形
     * */
    private void drawMask(Canvas canvas) {
        PorterDuffXfermode mode = new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);
        mBgPaint.setXfermode(mode);

        mBgPaint.setColor(Color.WHITE);
        float latticeWidth = width/10f;
        for (int i = 0; i <= 10; i++) {
            float startX = latticeWidth*i;
            Path path = new Path();
            path.moveTo(startX,-DensityUtil.dip2px(context,8));
            path.lineTo(startX+DensityUtil.dip2px(context,8),-DensityUtil.dip2px(context,6));
            path.lineTo(startX,DensityUtil.dip2px(context,8));
            path.lineTo(startX-DensityUtil.dip2px(context,8),DensityUtil.dip2px(context,6));
            path.close();
            canvas.drawPath(path,mBgPaint);
        }
    }

    public void setProgress(float progress) {
        this.progress = progress;
        invalidate();
    }
}
